package stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/5/6
 */
//3：筛选员工中工资高于 8000 的人，并形成新的集合
//6.获取员工工资最高的人
//7.计算Integer集合中大于6的元素的个数
//8.将员工按照工资由低到高排序，显示名字
//9.将员工按照工资由高到低排序，显示名字
//10.将员工按照工资由低到高（工资一样则按年龄由小到大）排序，显示名字
public class Test03 {
    public static void main(String[] args) {
        Employee e1=new Employee("张三", 7500,28);
        Employee e2=new Employee("李四", 8500,30);
        Employee e3=new Employee("王五", 9500,36);
        Employee e4=new Employee("赵六", 10000,29);
        Employee e5=new Employee("钱七", 8500,27);
        List<Employee> employees=Arrays.asList(e1,e2,e3,e4,e5);
        System.out.println("------------- 问题3：筛选工资>8000的员工------------");
        List<Employee> highSalary = employees.stream()
                .filter(e -> e.getSalary() > 8000)
                .collect(Collectors.toList());
        highSalary.forEach(e -> System.out.print(e.getName() +":"+e.getSalary()+"\t" ));
        System.out.println("\n");
        System.out.println("---------------问题6：工资最高的员工 ----------------");
        Double maxSalary=employees.stream().map(Employee::getSalary)
                .reduce(Double::max).orElse(0.0);
        System.out.println("工资最高为："+maxSalary);
        System.out.println("------------------- 问题8：工资升序排序 ----------------");
        employees.stream()
                .sorted(( em1, em2)-> {
                        return Double.compare(em1.getSalary(), em2.getSalary());
                })
                .map(Employee::getName)
                .forEach(n -> System.out.print(n + " "));
        System.out.println("\n");
        System.out.println("------------------问题9：工资降序排序 ------------------");
        employees.stream()
                .sorted((em1,  em2)-> {
                        return Double.compare( em2.getSalary(),em1.getSalary());
                })
                .map(Employee::getName)
                .forEach(n -> System.out.print(n + " "));
        System.out.println("\n");
        System.out.println("------------------问题10：工资+年龄排序 ----------------");
        employees.stream()
                .sorted(( em1,  em2)-> {
                        int salaryCompare = Double.compare(e1.getSalary(), e2.getSalary());
                        if (salaryCompare != 0) {
                            return salaryCompare;
                    }
                    return e1.getAge()-e2.getAge();
                })
                .map(Employee::getName)
                .forEach(n -> System.out.print(n + " "));
    }
}
class Employee {
    private String name;
    private double salary;
    private int age;
    public Employee() {
    }

    public Employee(String name, double salary, int age) {
        this.name = name;
        this.salary = salary;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return salary
     */
    public double getSalary() {
        return salary;
    }

    /**
     * 设置
     * @param salary
     */
    public void setSalary(double salary) {
        this.salary = salary;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Employee{name = " + name + ", salary = " + salary + ", age = " + age + "}";
    }
}
